well i’ve been working with docker for a lot of time, but i’ve always worked with containers with ports.
So now i have a concern, how works a container without port i mean. i have Mysql container but it works in 3606 port, but if i work with a container without port and i run it, it doenst run in docker ps.
I need information that how it works or something. Thanks.
I had tried with -it but it is the same, does not run.
And finally i did not get access with docker exec neither.
# docker container run -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql
starts a MySQL container without published ports and it is visible in the container list. Did you forget to set a password? Check the logs if it doesn’t work.
I gave this answer because you were speaking about MySQL and containers without ports. -e is used to set the environment variable.
I see now that your question is about something else, about the difference between run -it and run -d.
You have to know that a container runs only as long as a foreground process is running inside it. If you start (I use the long syntax and write -it like ‘interactive terminal’ because it’s easier to remember; not important at all)
docker container run -it google/cloud-sdk
then your terminal gets connected to the container and the process running in the foreground is the one you see at the bottom of the Debian Stretch image:
CMD ["bash"]
As soon as you type exit Bash exits too and the container is terminated. If you run
docker container run -d google/cloud-sdk
Bash has nothing to do and exits immediately together with the container. Check this with
docker container ls -a | grep cloud-sdk
This means if you want to run the cloud-sdk as a daemon so you are able to reconnect to it, add something like CMD ["tail", "-f", "/dev/null"] at the bottom of your Dockerfile and connect to the running container with