How do I enter this dockerfile / nginx container?
Asked Answered
C

3

29

With centos in a docker container, I just type 'docker attach container ID' and it takes me to the shell prompt, where i can install and configure nginx.

This one is easier: docker.com dockerfile/nginx You just run the file and everything is installed and configured.

but i can't figure out how to get in and access the files.

Chlamys answered 24/9, 2014 at 9:5 Comment(0)
B
42

In my case the standard bash didn't exist. Using /bin/sh helped me:

docker run -it -p 80:80 dockerfile/nginx /bin/sh
Blueing answered 9/7, 2019 at 13:59 Comment(0)
S
30

UPDATE (a much easier method that was introduced later):

docker exec -t -i container_name /bin/bash

Original answer

Actually you can access a running container too.

Find your container's ID:

docker ps

Export the ID of the process that runs the container:

PID=$(docker inspect --format '{{.State.Pid}}' my_container_id)

"Connect" to it by changing namespaces:

nsenter --target $PID --mount --uts --ipc --net --pid

Originally this was described here: http://jpetazzo.github.io/2014/03/23/lxc-attach-nsinit-nsenter-docker-0-9/

Sannyasi answered 4/10, 2014 at 21:23 Comment(1)
My docker instance was built based on busybox and it didn't have /bin/bash so I just used docker exec -t -i container_name sh and it did workDisk
G
6

First make sure to understand the difference between images and containers. Running the image:

docker run -d -p 80:80 dockerfile/nginx

creates a new container executing only nginx. This process does not interact like a shell. If you really need access to the files in this container while its running, your only option is to use nsinit, nsenter or lxc-attach. Have a look at https://blog.codecentric.de/en/2014/07/enter-docker-container/ for details.

Alternatively, you might want to try

docker run -it -p 80:80 dockerfile/nginx /bin/bash

which creates a new container executing an interactive shell instead of nginx.

Greenberg answered 24/9, 2014 at 10:31 Comment(2)
docker run -d -p 80:80 dockerfile/nginx /bin/bash this gave me an error Error response from daemon: Cannot start container a126421fd50a64a939265019b64f47d86bc54a82f4dd4c29088da4e8d7f15c33: Bind for 0.0.0.0:80 failed: port is already allocated so I stopped the container first and tried it again. it worked. I got new container So I tried to attach to it but here's another error. You cannot attach to a stopped container, start it first I can restart but it stops immediately.Chlamys
There was a typo in the anwer. For /bin/bash the command line parameters should be -it and NOT -d (daemon). Corrected that in my answer.Greenberg

© 2022 - 2024 — McMap. All rights reserved.