In the article you will learn how to run and manage a docker container on Linux Ubuntu and similar distros. You will also be able to push a new image to Docker Hub.
You would need to have Docker installed and configured properly beforehand. We have written a detailed article on how to achieve this here.
An application is actually isolated when it is run via a Docker container and can therefore be configured without consuming extensive system resources. When it comes to virtual machines however, we are dealing with a whole environment (.i.e. server) and all of its maintenance and environment related issues which require additional computing power.
In a virtual machine, a fully operational OS must be available whereas for a Docker container, parts of the OS and the kernel are shared with other system resources which makes it easy to create and remove Docker containers unlike virtual machines which need a complete installation.
Since containers are lightweight and do not require extensive system resources, they can run in parallel in a single machine but they cannot be migrated during the execution unlike virtual machines which can be migrated while executing .
Containers, much like virtual machines, allows users to interact with them. As an example of this powerful feature, we can create a fully fledged Ubuntu machine by running a container using Ubuntu image as follows:
docker run -t -i ubuntu /bin/bash
The output should look similar to the following :
You will notice that the command prompt has changed which means you are currently working inside a Docker container. You should now be able to issue commands, for instance you can execute the ls command which will run inside the prompt root@44bfd4fc4ww3:/# . You can run any command without the sudo since within a container , you are working as a root user. In case you install packages for instance, it will only apply to that container.
If you suddenly exit this prompt using exit command, you will return to the previous prompt from which you came from. Note that once you exit the docker container, it will no longer be available.
The container id that is shown in the command prompt, in the example above 44bfd4fc4ww3, will be useful later in order to identify the container in case you want to get rid of it.
If you have run Docker containers for some time, you should be able to see currently running or active containers as well as inactive ones. In order to display the active and inactive containers, execute the command :
docker ps -a
You should be able to see an output that is similar to:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
44bfd4fc4ww3 ubuntu “/bin/bash” 27 minutes ago Exited (0) 10 minutes ago villain_pollard
When you don’t ascribe a name to the container that you created, docker will assign a random name. On the example above, this was villain_pollard. If you created an Ubuntu image container, you will be able to find it using the image name ubuntu. When you start a new container, you can give it a name using the switch –name.
To see the most recently created container, use the -l switch as follows:
docker ps -l
To stop an active container, use the command ‘docker stop’ followed by the ID or name of the container. For example :
docker stop 44bfd4fc4ww3. [stop docker container]
If you have stopped a container, you should be able to start it again using the ‘docker start’. You will need the container ID that was mentioned above.
If for example we want to start the container (Ubuntu related) with the ID of 44bfd4fc4ww3, run the command below:
docker start 44bfd4fc4ww3 [start docker container]
This will start the corresponding container. To see its status, use ‘docker ps’ command.
To remove a container you no longer want to use, you would need to run the docker rm command using either the ID or the name of the container. To find the ID, issue the docker ps -a command. Once you have the ID, run the rm command as follows :
docker rm 44bfd4fc4ww3 [remove docker container]
To create a container that will self destruct when it is stopped, you can use the –rm switch.
When working within a container, the changes that are made will only apply (and limited) to that container. As you have seen above, it is possible to start and stop a container. When the container is removed However (using ‘docker rm’ command), all the changes that have been made within it will be lost .
When you have an Ubuntu container for instance and you install mySQL, you will end up having a container that runs off an image. If you want to reuse the mySQL container in new images later on, you would have to perform a commit to a new docker image as follows :
docker commit -m “installed mySQL” -a “name of author” container_id repository/NewImage_name
Where the -m option is used to indicate a commit message (much like in Git) in order to help other users know the changes that were made.
The author name is specified using the -a switch. The container_id is the identifier of the container that has been mentioned earlier in this article. The repository is (usually) your Docker Hub username (unless you added other repositories on Docker hub).
For the user net2adm for instance and the container ID i.e. 44bfd4fc4ww3, the command above would become:
docker commit -m “installed mySQL” -a “net2adm” 44bfd4fc4ww3 net2adm/ubuntu-mysql
When you perform a commit on an image, the new image will be saved locally on your machine. In order to let others access this new image however, you would need to carry out a push to Docker Hub or any other Docker registry. This will be covered later in this article.
The new image will show up along with the other Docker images when you run the command :
REPOSITORY TAG IMAGE ID CREATED SIZE
net2adm/ubuntu-mysql latest 4b4e47014wd9 1 minute ago 250MB
ubuntu latest 44bfd4fc4ww3 1 hour ago 81.2MB
In the example above, ubuntu-mysql is the new image which was based on the already existing ubuntu image from Docker Hub. The difference here is that mySQL was installed (see the size). When you need to use mySQL, you do not need to create an Ubuntu image from Docker Hub and then re-install mySQL again but rather you would simply have to use the new image above.
Now that you created a modified image from the one existing on Docker Hub, you can share it with other users. In order to push an image to Docker Hub, you would need to have an existing account there. Follow this link to create an account on Docker.
Before pushing your new image, you would first need to log into Docker Hub by running the command :
docker login -u your_docker_username
You will be prompted to enter your Docker login credentials. Once authenticated successfully, you will see a login success message.
Now to push your new image, run the command :
docker push your_docker_hub_username/ubuntu_mysql
This may take some time to finish. Once completed, login to your account on Docker and you should be able to see the new image listed on your dashboard.
In this article, you have seen how to run and manage Docker containers. You have learned how to create new images and how to push them to Docker Hub.
If you like the content, we would appreciate your support by buying us a coffe. Thank you so much for your visit and support.