简体   繁体   中英

Docker on windows can mount a folder for nginx container but not for ubuntu

I am building an image from this docker file for NGinx

FROM nginx
COPY html /usr/share/nginx/html

I then run the container using this command

docker run -v /C/nginx/html:/usr/share/nginx/html -p 8081:80 -d --name cntr-mynginx mynginx:abc

This works and I am able to mount the folder and the changes made in the html folder on the host can be seen when within the container file system. The edits made on the container filesystem under the /usr/share/nginx/html folder are visible on the host as well.

Why does the same not work when I use an Ubuntu base? This is the docker file for the Ubuntu container I am trying to spin up.

FROM ubuntu:18.04
COPY html /home

I used this command to run it

docker run -v /C/ubuntu-only/html:/home -p 8083:8080  --name cntr-ubuntu img-ubuntu:abc

The command above runs and when I do a docker ps -a , I see that the container stopped as soon as it started.

I removed the copy of the html and made the ubuntu image even more smaller by keeping just the first line FROM ubuntu:18.04 and even then I get the same result. Container Exited almost soon as it started. Any idea why this works for NGINX but not for Ubuntu and what do I need to do to make it work?

The issue you are experiencing does not have to do with mounting a directory into your container.

The command above runs and when I do a docker ps -a, I see that the container stopped as soon as it started.

The container is exiting due to the fact that there is no process being specified for it to run.

In the NGINX case, you can see that a CMD instruction is set at the end of the Dockerfile.

CMD ["nginx", "-g", "daemon off;"]

This starts NGINX as a foreground process, and prevents the container from exiting immediately.

The Ubuntu Dockerfile is different in that it specifies bash as the command the container will run at start.

CMD ["/bin/bash"]

Because bash does not run as a foreground process here, the container exits immediately.

Try augmenting your docker run command to include a process that stays in the foreground, like sleep .

docker run -v /C/ubuntu-only/html:/home -p 8083:8080  --name cntr-ubuntu img-ubuntu:abc sleep 9000

If you run docker exec -it cntr-ubuntu /bin/bash you should find yourself inside the container and verify that the mounted directory is present.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM