Running an interactive Ubuntu container

dockerThis article describes how to start up an Ubuntu container. Once it is started you can run any commands as you would on a normal Ubuntu server.


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
Digest: sha256:5f4bdc3467537cbbe563e80db2c3ec95d548a9145d64453b06939c4592d67b6d
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:

root@07b6a84211b5:/#

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

root@07b6a84211b5:/#

Ubuntu commands

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.

root@07b6a84211b5:/# exit

steven@localhost ~

 

Thanks for visiting.