Docker Development: Frequently Asked Questions and Solutions
Do you have a Docker in a development environment or a test environment? If there is, then I guess you must have encountered a lot of pits, this article summarizes the common problems of the solution, hoping to help you. Note that some of the issues apply only to VirtualBox.
The port is forwarded to the host
Port forwarding can only be used under the Boot2Docker VM. If you also want to use the same port to forward to your host, such as you are testing the background of Android applications or want to share VM?
You have two options:
- To the VirtualBox settings to add a Bridged adapter. Now when you start docker-machine again, your VM will get a formal LAN address. At this point you can access the virtual machine from any other device on the network, and docker-compose port forwarding can also be used.
- Wait until Issue # 691 (or related) is resolved. You can allow you to use SSH port forwarding. The only problem is that you have to manually operate every port you need.
Docker-machine ssh -L <host-port>: localhost: <machine-port>
By default, when you use the docker-machine to create a VM, only get very low configuration (1CPU, 1GB RAM). Run "docker-machine help create" to understand the corresponding flag, you can use them to increase the CPU / RAM. E.g:
Docker-machine create \
--driver virtualbox \
- virtualbox-cpu-count 2 \
- virtualbox-memory 2048 \
Symbolic link error
There are a variety of reasons for this error, but the most common one is VirtualBox missing Guest Additions . Make sure it is installed (VirtualBox> Preferences> Extensions) and match the version of VirtualBox. This mostly occurs when VirtualBox is installed separately.
Shared folders can not be updated
In Boot2Docker, this is a known bug . Because it only caches NFS files in order to consider the speed of the upgrade. So you need to refresh the file system cache.
Docker-machine ssh <name-of-your-machine>
# Echo 3> / proc / sys / vm / drop_caches
Some commands / languages ignore the FS cache. For example: when running npm, less, vi and so on, you will not find these errors. But other commands use cache -cat, python, Apache SendFile, and so on.
In VirtualBox, this issue also involves the Apache SendFile bug .
Sometimes you will see strange network errors, like the address can not be resolved, can not ping 通 and so on. Usually because the host's network configuration has been changed (for example, you switch to the WiFi network, etc.), then you can restart the VM to get updated network configuration.
Build and correlate
Your Dockerfile is usually used to compile and build applications and is ready to boot. When you load your folder with Volume (as is usually the case in development), this can cause various problems.
- Some languages (Ruby, Python, etc.) are installed depending on the shared directory of the user directory. The other (Node / NPM) is installed directly in the current application folder. So, although there are "npm install" when building the container, but later when you run the shared volume with "docker-compose up", the "node_modules" folder disappears!
- Similarly, the build folder is gone. As a result, the CMD used to start the application will not work in most cases.
It's tricky even if you are using different Dockerfile in dev and prod. Because this is not a build image of the problem, but a container runtime problems. The current solution depends on your language (rake / grunt / gulp / fabric / etc) corresponding Task runner, and in Docker's CMD to use it to develop. Do not fill in the application's "build" logic in Dockerfile, but instead use the task runner.
# Gulp / grunt / rake / ...
# Have a "build" task
# Used for normal production deployment
# Here you use separate tasks as needed
COPY package.json / myapp /
RUN npm install
COPY. / Myapp
RUN gulp build
CMD ["npm", "start"]
# When developing, combine all the above commands
Volumes: ".: / Myapp"
Command: "sh -c 'npm install && gulp build && npm start'"
Make sure your ".dockerignore" file can ignore common special files when cross-platform. These unwanted files will be invalidated by Docker's build cache. Have the correct ".dockerignore" file, build when you can simply run "COPY ./myapp" to copy your entire app source code instead of a folder and another file to copy.
# Ignore .git folder
# Ignore all Windows, Linux and OSX special files
# Https://www.gitignore.io/api/windows,linux,osx, vim
# Ignore all your language-specific build folders
Use gitignore.io to get a list of common files for the corresponding language / platform. Keep in mind, however, that "gitignore" and ".dockerignore" differentiate these files in different ways. Dockerignore requires that the entire file name must match. So in gitignore, you can write "node_modules" to ignore the entire folder, but in dockerignore you have to use "node_modules / *".
The following docker-clean command can remove all tags without tags and stop the container. You can run the command periodically to clean up the VM.
Docker ps -aqf status = exited | xargs docker rm
Docker images -qf dangling = true | xargs docker rmi
You can use a separate docker-compose.test.yml, from the usual "docker-compose.yml" to " extend " the service, but this is simply overwriting the "command" set to run the test. Then on CI, run the following command:
Docker-compose -f docker-compose.test.yml run <service_name>
This will start all the link services directly and run the tests and then exit with your test script's status code. Now that your CI can simply install the docker-compose and can run the above command for each service without having to understand the specific service!
When running the test, docker-compose is easy to link all the micro services together. For pet projects, this is usually not a problem, but for larger projects is a headache. Avoid it as early as possible, you can use stub servers like stubby4node to use stubs with a simple YAML file that describes stubbed endpoints.
Clock synchronization error
When the docker-machine VM's clock is not synchronized with the actual time, you will get a lot of strange errors such as signature errors (various AWS services). In this case, simply restart the VM to synchronize the clock. This usually happens when pausing / resuming your laptop or desktop. Your host operating system has updated the time, but the pending VM is lagging behind. The same is true for Vagrant.
Forget to turn off the virtual machine
"Shameless plug": When we are developing a menu bar on the Docker application, we often leave the docker-machine VM to keep running, forget to turn it off. Take a look at: github.com/rdsubhas/docker-menu !
Docker for Development: Common Problems and Solutions (translated by Bruce.Chen proofreading: Li Yingjie)