Start the created container and attach to it
In the previous step, you created a Docker container named my-ubuntu-container
but did not start it. In this step, you will learn how to start this created container and attach your terminal to its standard input, output, and error streams.
To start the container, use the docker start
command followed by the container name.
docker start my-ubuntu-container
This command starts the container. However, it runs in the background by default. To interact with the container, you need to attach to it.
To attach to the running container, use the docker attach
command followed by the container name.
docker attach my-ubuntu-container
After running this command, your terminal will be connected to the container's shell. You should see a command prompt that looks like the one inside the Ubuntu container, typically something like root@<container-id>:/#
.
Now that you are inside the container, you can execute commands within it. For example, let's check the operating system version.
lsb_release -a
You should see output showing the Ubuntu version running inside the container.
To exit the container without stopping it, press Ctrl + P
followed by Ctrl + Q
. This detaches your terminal from the container, leaving the container running in the background.
If you simply type exit
or press Ctrl + D
while attached, the container will stop.
After detaching, you can verify that the container is still running by listing the running containers.
docker ps
You should see my-ubuntu-container
listed with a status indicating that it is running.