Docker Home



So, if you are new to Docker, you might wonder how to run a docker container. Let me quickly show you that.

You can create and run a container with the following command:

Create your own secure Home Network using Pi-hole and Docker Last Updated: 26 May, 2020 Pi-hole is a linux based web application, which is used as a shield from the unwanted advertisement in your network and also block the internet tracking system.This is very simple to use and best for home and small office networks. Docker Desktop for WSL 2 Windows Home is a full version of Docker Desktop for Linux container development. It comes with the same feature set as our existing Docker Desktop WSL 2 backend.

  1. May 10, 2016 The only option for Home edition users is to use Docker Toolbox. So yes, this is still valid. Displac3r (Displac3r) November 13, 2018, 8:59pm.
  2. The application works with Docker Compose. Use you own or third-party docker-compose.yml configs. DockStation generates a clean and native docker-compose.yml file which can be used even outside the application, using the native Docker Compose CLI commands.

And then, if you want to enter the container (to run commands inside the container interactively), you can use the docker exec command:

Docker Registry. This page contains information about hosting your own registry using the open source Docker Registry. For information about Docker Hub, which offers a hosted registry with additional features such as teams, organizations, web hooks, automated builds, etc, see Docker Hub.

Here’s an example where I create a new container with Ubuntu as the base image and then I enter the running Ubuntu container and run the ls command:

Lost? Don’t worry. I’ll explain in detail what the above two commands do and what is the -it option in the docker run and exec command.

How to run docker container

If you want to run a docker container with a certain image and a specified command, you can do it in this fashion:

The above command will create a new container with the specified name from the specified docker image. The container name is optional.

Docker
  • The -i option means that it will be interactive mode (you can enter commands to it)
  • The -t option gives you a terminal (so that you can use it as if you used ssh to enter the container).
  • The -d option (daemon mode) keeps the container running in the background.
  • bash is the command it runs.

The reason for running bash as command here is that the container won’t stop immediately.

In the above example, I didn’t name the container so it was randomly named determined_blackburn.

And as you can see, the container is running bash command in the background.

What happens if you don’t run it as a daemon (-d option) in the background?

If you do not use the -d option, docker run will create a new container and you’ll have a terminal in interactive mode running bash shell.

As you can see in the example below, the container is created and I am automatically inside the container (bash shell).

The problem here is that if you exit the container, the container stops.

What happens if you only use the -i (interactive) option?

Let’s say you only use the -i option. In that case, you’ll see an interactive prompt but if you exit the interactive prompt (using Ctrl+D or exit command), you exit the container and the container stops.

Docker Home

What happens if you only use the -t (terminal/tty) option?

That’s a tricky situation because in that case, you’ll have a pseudo terminal (if you use the -t option) but you won’t have the STDIN. In other words, you cannot enter anything, cannot run command inside the container like you did earlier:

I had to stop the container from another terminal in the above case.

How to run an existing container

The docker run command creates a new container from the specified image. But what happens when you already have a container?

If you want to run an existing container, you must first start the container and then you can use the exec option like this:

This example will be better for your understanding:

Why use bash all the time?

In all the above examples, I have used bash or /bin/bash as the command that runs with the container. I used it because it gives a shell and when you run the container, thanks to the shell, you can run regular commands inside the container as if you are inside a regular Linux system.

You can ask the container to run any command but keep in mind that the container exists as soon as the command completes.

As you can see in the example below, there is no interactive terminal session this time. You are not ‘inside’ the container anymore because the echo command finishes almost immediately.

You can in fact run other commands as well and enter the container afterwards.

In the example below, I created a new container that runs nginx server on port 80. Since I run it as a daemon with option -d, the nginx container keeps on running.

And then I use the docker exec command to get an interactive bash shell and thus enter inside the nginx container (which is basically a Linux preconfigured with nginx).

I hope you have a better understanding about how to run docker containers and why it uses certain options.

If you have questions or suggestions, do let me know in the comment section.

Become a Member for FREE
Docker

Docker Homepage

Become a member to get the regular Linux newsletter (2-4 times a month) and access member-only contents.

Docker Home Assistant

Join the conversation.