preface

Install a variety of development packages every time you change the computer, electron packaging, network reasons can not rely on a variety of downloads, application deployment difficulties? Take a look at this walkthrough to create a portable development environment once and for all

Docker installation

  • Official website registration account
  • MAC Win: Directly download the client to run
  • Linux installation: hub.docker.com/search?q=&t…

Docker basis

images

  • Docekr pull [images] #
  • Docekr imgaes # view all images
  • Docker rmI [imageID] # delete specified image
  • Docker rmi-f \$(docker images-aq) #! Tip Delete all mirrors

container

  • Docekr ps # view the running container
    • -a Lists the history
    • -n Lists the latest
    • -q Displays only numbers
  • Docker rm [containerID] # delete the container
  • Docker rmi-f \$(docker ps-aq) #! Tip Delete all containers
  • Docker start id #
  • Docker restart id #
  • Docker stop id # Stop the currently running container
  • Docker kill ID # force to stop the currently running container

network

  • Bridge: Docker (default)
  • None: The network is not configured
  • Host: The host mode shares the network with the host
  • Container: Container network connection (rarely used! Limited)
  • Create a user-defined network
    • Docker network create --driver bridge --subnet 192.168.3.0/16 --gateway 192.168.3.1 mynet
  • Viewing the Network List
    • docker network ls
  • Remove the network
    • docker network rm ID

Creating a startup container

  • Docekr run [parameter] imagesID # Creates a container and starts it

    • -it Runs in interactive mode without entering the container to view the contents
    • -d Background running
    • --name=" name "Container names distinguish containers
  • Port mapping: Maps container service ports to host ports

    • -p [host port]:[Container port]
    • -p Random port
  • Data volume: Data is shared between hosts and containers

    • -v [container path] Mount anonymously
    • -v [host path]:[Container path] Named mount
  • Network: Select the network mode

    • -net [name]
  • Example: docker run -itd -p 9000:9000 –name demo -v /var/run/docker.sock:/var/run/docker.sock -v /Users/luofei/learn/docker_file/portainer/data:/data docker.io/portainer/portainer /bin/bash

View basic container information

Docker inspect Container ID

Viewing container Logs

Docker logs container ID

Copy from container to local machine

  • ‘docker cp native path container ID: file path // host content copied to container ‘
  • ‘docker cp container ID: file path native path // container copy content to host ‘

Enter a container

  • docker exec -it [container] /bin/bashExecute commands in an already running container, and exit the shell without creating and starting a new container will not cause the container to stop running
  • docker attach [container]The local input goes directly to the container, and the output of the container is displayed directly on the local screen. If you exit the shell of the container, the container will stop running

Out of the container

  • Exit Direct exit
  • Ctrl + P + Q exits without stopping

Make a mirror image

Docker commit -m=” commit description “-a=” author”

DockerFile

  • FROM: Who is the mother of this mirror (basic mirror)
  • MAINTAINER: Who wrote this image (MAINTAINER information)
  • RUN: command to RUN to build an image
  • ADD: Step: Copu files will be automatically decompressed
  • WORKDIR: Sets the current working directory
  • VOLUME: configures coupons to mount host directories
  • EXPOSE: Exposes the port
  • CMD: specifies that only the last command to run when the container is started is valid and can be replaced
  • ENTRYPOINT: Specifies the command to run when the container is started. You can append commands
  • ONBUILD: The ONBUILD command is run when building an inherited DockerFile, triggering the command
  • COPY: similar to ADD copies our files to the image
  • ENV: Set environment variables when building the image

Release the mirror

Docker hub

  • docker loginThe login
  • docker tag [imageID] [message]Image label
  • docker push [image]Upload the image

Ali cloud

  • Log in to Aliyun

  • The container image service is displayed

  • Creating a namespace

  • Creating a Mirror repository

    $ sudo docker login --username=username registry.cn-hangzhou.aliyuncs.com $ sudo docker tag [ImageId] Registry.cn-hangzhou.aliyuncs.com/jackfly/jackfly: [the mirror version number] $sudo docker push Registry.cn-hangzhou.aliyuncs.com/jackfly/jackfly: [the mirror version number]Copy the code

Create an exclusive system image

Whatever’s missing, this is good enough for the front end.

  • Choose ubuntu16.04 (personal preference)
  • net-tools
  • SSH service (connect docker container through shell tool)
  • Node: v12.18.1
  • NPM: 6.14.5
  • Pm2:4.4.0
  • Git: 2.7.4
  • ZSH: 5.1.1
  • zsh_plugs:(git,zsh-autosuggestions, zsh-syntax-highlighting)
  • Nginx

Dockerfile instance

Dockerfile on github: github.com/JackFlyL/do…

Generate the mirror

docker build -f dockerfile -t [imageName] .
Copy the code

Attention!! Do you have to add the “.”?

Image using

docker run -itd --name mywork -v /Users/jackfly/docker:/home/work -p 6555:22 -p 8080:80 mywork 
Copy the code
  • Use oh-my-zsh, this thing is really love.
docker exec -it mywork /bin/zsh
Copy the code
  • When shell connected to docker container RUN, we mapped two ports. 22 is the port of SSH service, so 6555 mapped by the host can be connected to docker inside. The default password is root

Connection successful! This is your own server, you can make it whatever you want, more freely than docker data volume sharing.

About Application Deployment

The web page

  • Nginx proxy, mapping port to host, accessed through host IP: mapping port
  • Node. js starts the Pm2 process to daemon port mapping to the host, which is accessed through host IP address: Port mapping

Electron application

Package generated APP is directly used by sharing data to host through data connectivity

docker run -itd -v /home/data:/home/AlectronApps mywork
Copy the code

conclusion

So far a portable development environment is done, even if you go to any system any computer as long as docker support you can pull the image to restore their working state, and fault tolerance rate is high, reinstall the system only need to run again. You can put your own projects into the mirror and update the mirror continuously. Basic image dockerfile on Github, students need to take their own. Mirror IMAGE I also uploaded to Ali cloud, do not want to toss lazy people also directly use ha!

  • Github:github.com/JackFlyL/do…
  • Ali cloud image package: docker pull registry.cn-hangzhou.aliyuncs.com/jackfly/mywork:1.0