Running an interactive Ubuntu container
docker run command
steven@localhost ~ $ sudo docker run --name ubuntu-container -i -t ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
6b98dfc16071: Pull complete
4001a1209541: Pull complete
6319fc68c576: Pull complete
b24603670dc3: Pull complete
97f170c87c6f: Pull complete
Status: Downloaded newer image for ubuntu:latest
This will execute the docker run command which creates and runs a new container. The --name flag allows us to give it a meaningful name. The -i flag keeps the STDIN open making this interactive even if we aren't attached to the container. The -t flag assigns a pseudo-tty connection allowing us to attach to an interactive shell in the newly created container. We asked Docker to use the ubuntu image, it then defaults to 'ubuntu:latest' and searches for it on our host. In its absence it downloaded it. Finally we told Docker to run /bin/bash to launch us into the Bash shell, which presented:
This shows that we are now logged on as root in an ubuntu container! Importantly we can use container names and container IDs interchangebly.
If we wanted to just create the container (and not run it initially) we could type the following:
steven@localhost ~ $ sudo docker create --name ubuntu-container -i -t ubuntu /bin/bash
With the container created, we could start it and then attach to the interactive session as needed:
steven@localhost ~ $ sudo docker start ubuntu-container
steven@localhost ~ $ sudo docker attach ubuntu-container
Once you are logged on to the ubuntu-container, you can run some typical Ubuntu commands. For example this will install my favourite text editor:
root@07b6a84211b5:/# apt-get update
root@07b6a84211b5:/# apt-get install vim
Stopping the container
You can type exit to stop the container and return to the local user on your Docker host.
Thanks for visiting.