:/# echo "inside existing container"ī6b79a9f7789 ubuntu "bash" 8 minutes ago Up 43 seconds loving_galileo This example will be better for your understanding: :~$ docker ps -aī6b79a9f7789 ubuntu "bash" 7 minutes ago Exited (0) 7 minutes ago loving_galileo If you want to run an existing container, you must first start the container and then you can use the exec option like this: docker start existing_container_ID_or_nameĭocker exec -it existing_container_ID_or_name /bin/bash But what happens when you already have a container? The docker run command creates a new container from the specified image. I had to stop the container from another terminal in the above case. In other words, you cannot enter anything, cannot run command inside the container like you did earlier: :~$ docker run -t ubuntu bash That’s a tricky situation because in that case, you’ll have a pseudo terminal (if you use the -t option) but you won’t have the STDIN. :~$ docker run -i ubuntu bashī6b79a9f7789 ubuntu "bash" 27 seconds ago Exited (0) 9 seconds ago loving_galileo What happens if you only use the -t (terminal/tty) option? In that case, you’ll see an interactive prompt but if you exit the interactive prompt (using Ctrl+D or exit command), you exit the container and the container stops. :/# echo this is a new containerĨ2766613e7bc ubuntu "bash" 2 minutes ago Up 2 minutes determined_blackburnĦ098c44f2407 ubuntu "bash" 27 seconds ago Exited (0) 8 seconds ago bold_benzĨ2766613e7bc ubuntu "bash" 2 minutes ago Up 2 minutes determined_blackburn What happens if you only use the -i (interactive) option? The problem here is that if you exit the container, the container stops.
If you do not use the -d option, docker run will create a new container and you’ll have a terminal in interactive mode running bash shell.Īs you can see in the example below, the container is created and I am automatically inside the container (bash shell). What happens if you don’t run it as a daemon (-d option) in the background? In the above example, I didn’t name the container so it was randomly named determined_blackburn.Īnd as you can see, the container is running bash command in the background. :~$ docker run -it -d ubuntu bashĨ2766613e7bc0ae98887bb1c776fa0de7f3c4771db9ac7ffc5db5c2c68ab497bĬONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESĨ2766613e7bc ubuntu "bash" 4 seconds ago Up 3 seconds determined_blackburn The reason for running bash as command here is that the container won’t stop immediately.
The -i option means that it will be interactive mode (you can enter commands to it).The above command will create a new container with the specified name from the specified docker image. If you want to run a docker container with a certain image and a specified command, you can do it in this fashion: docker run -it -d -name container_name image_name bash I’ll explain in detail what the above two commands do and what is the -it option in the docker run and exec command.
:~$ docker exec -it my_container bashīin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var Here’s an example where I create a new container with Ubuntu as the base image and then I enter the running Ubuntu container and run the ls command: :~$ docker run -it -d -name my_container ubuntu bashħe77640bca686108ad415318278a1fa148e1c84d3b180276e19ec2b970ba9b67 You can create and run a container with the following command: docker run -it -d -name container_name image_name bashĪnd then, if you want to enter the container (to run commands inside the container interactively), you can use the docker exec command: docker exec -it container_ID_or_name /bin/bash
#DOCKER RUN IMAGE IN CONTAINER HOW TO#
Type exit to leave the shell session.So, if you are new to Docker, you might wonder how to run a docker container. ls / will list the contents of the root director in the container, ps aux will show running processes in the container, cat /etc/issue will show which Linux distro the container is running, in this case Ubuntu 18.04.
Run the following commands in the container. How can I tell if Docker is installed on Linux?.How do I open Docker in Linux terminal?.