Docker containers are built from Docker images, which are pulled from Docker Hub. Docker Hub is a registry managed by Docker.
You are able to host your Docker image on Docker Hub, so most applications and Linux distributions you’ll need will have images hosted there.
To work with Docker containers, you can use the Docker CLI.
For this tutorial, we are using an AlphaVPS service with Ubuntu 22.04 with a sudo user and pre-installed Docker. If you require assistance with your Docker installation, you can refer to our guide here.
Creating a container
You are able to search for docker images on Docker Hub by using the
docker search command. An example command would be:
sudo docker search ubuntu
You will receive an output that matches all available results for Ubuntu.
To run any of these images, you can run the following command:
sudo docker run `name`
Don't forget to replace the actual name.
If Docker is unable to find the desired template locally, it will try to pull it from a remote source. By default, your remote source is Docker Hub.
Of course, you are also able to pull templates locally on your server. Simply run:
sudo docker pull `name`
Wait for the pull request to finish.
Using default tag: latest latest: Pulling from ubuntu/postgres 61418eecaecf: Pull complete a26d94fc7ebc: Pull complete 976a671da9a7: Pull complete 4b7d77a0004a: Pull complete 3c0a58ce45b5: Pull complete ddc9cd9f4e99: Pull complete edbb31d503cf: Pull complete f02420596052: Pull complete 7bd50bbbc683: Pull complete cab44f9a3dbd: Pull complete Digest: sha256:915dfa8a0581fcb0ba3fcb06843a3e9482ab2e7538a8638a8b41ba0cd5598a5a Status: Downloaded newer image for ubuntu/postgres:latest docker.io/ubuntu/postgres:latest
To view all of your local images, you need to run:
sudo docker images
Managing docker containers
Containers resemble servers but are way-more resource friendly.
As servers, you are able to interact with them similarly to how you interact with your own service.
You are able to list your docker containers by running the following command:
sudo docker ps
To view all containers, including inactive ones, you can run:
sudo docker ps -a
If you already have containers on your system, your output will look like this:
By listing the containers, you are provided with detailed information about each one of them.
CONTAINER ID or
NAME of each container is what you will be looking for in most cases. They allow you to directly refer to the container and manipulate it.
Accessing a container
By adding the
-it flag to the
docker run command, you are able to launch an interactive shell for the container itself.
An example command would be:
docker run -it ubuntu
Once the command is executed, you will notice that your prompt changed to something like
9944bd734e3c string is a randomly generated name for your container. You are now able to execute commands directly in your container.
You can exit the container by typing
exit. Do note that typing
exit will also stop the container. To exit the container without turning it off, you can press
If your container is already started and you simply want to attach to its shell, you can use the following command:
docker attach `container-id-or-name`
You will once again notice that your prompt reflects this.
Another option is to execute a command directly without attaching to the shell of the container. The
exec command allows you just that.
docker exec `container-id-or-name` `command`
An example command is
docker exec 5f033096907f ls
All of these utilities provide a wide range of options, which can be displayed by running their respective command with the
Start & Stop
Docker container can be easily started and stopped by using the commands below:
docker stop `container-id-or-name` docker start `container-id-or-name`
Removing a container
Once you no longer need a container, you can remove it with the
docker rm command:
docker rm `container-id-or-name`
If you are interested in how to commit your changes on the container to the actual image, and push it to Docker Hub, you can continue with our guide here.