In this video, you'll learn how to run containers in either Docker or Popman.
So the Popman command on Red Hat is fully compatible with the Docker command.
And if you're on Red Hat and you want to follow along exactly with the Docker command, there's
an easy solution.
Use export alias Docker is Popman.
And unless you are a very advanced user, you won't notice any difference.
To run a container, the basic idea is the command docker run image, where image refers
to the name of the specific container image that was found on Docker Hub.
Docker Hub is a standard container registry that Docker is using.
Check it out on hub.docker.com.
It has a website that allows you to search for container images.
The run command is commonly used with options minus D, which runs the container in detached
mode.
Dot minus D is going to occupy your screen and you don't see anything else.
Minus IT is commonly used to run a container in interactive terminal mode.
That is what you want to do if you want to run a shell on the container you just started.
And to get an overview of all containers that you just started, you use Docker PS, optionally
with the minus A option, which will also show you containers that have been started in the
past and which are now no longer running.
Let me show you.
So we have already done a docker run minus D nginx.
And if at this point I'm using Docker PS, then I can see my currently running container.
The container has an ID, which I can use for managing.
It uses a standard command, which is normally a script with the name docker entry point,
which is going to take care of what exactly you want to do.
It's exposing its services on the port, and it has an automatically generated name that
you might want to use for managing.
If I would use docker run minus IT BusyBox, there we have another example.
I'm using the BusyBox container image, which is a commonly used container image.
It's commonly used because it's so small.
It's a minimal Linux distribution that fits in just a couple of megabytes.
Now here you can see that suddenly I'm in a root shell, and that is because I'm within
the BusyBox container image.
I would advise if you're ever wondering where you are, check out the OS release file.
And oh boy, BusyBox is so small, it doesn't even have an OS release.
OS release is the file that's the Linux Foundation standard.
It should be there on all Linux distributions to identify the distribution where you are.
Now here I'm in the shell that was started by BusyBox.
If I'm going to use exit, I'm exiting that shell.
But then when I'm using docker ps, I don't see BusyBox anymore.
That's because the shell was the primary command, and if you exit the container primary command,
it's gone.
So now I need docker ps minus a, and docker ps minus a is showing all of them, including
containers that were exited.
Now when containers are running, you can manage them.
In order to do so, there's a couple of commands involved.
Starting with docker inspect, which shows properties of your containers.
Docker stop will stop a running container.
And docker exec minus t, container name slash bin slash bash, opens a shell in a currently
running container.
And that is useful for debugging.
Let me show you.
So I want to inspect my quirky query, which is the nginx container.
Docker inspect qutab will do it for me.
And here we can see in JSON format everything that is going on in the container.
An important part is the path, path is the default application, which was started with
these arguments.
And here you can see that it has started nginx.
And if you scroll further down, you can find all the properties of the container.
And that is really useful if your container isn't doing what you want it to do.
Now this quirky query container is running in the background.
And I can connect to it using docker exec minus it for the interactive terminal on a
quirky query.
And I'm going to run slash bin slash bash.
Now one thing you should notice, not all containers have bin bash on board, because containers
are often used as minimal environments.
And if it's giving an error message on bin bash, I would advise you use bin sh instead.
So now I'm inside the container, and I can check, for instance, which processes are running.
Well, that doesn't work exactly for the simple reason that, as you can see here, the ps command
is not found.
That's the nature of containers.
Containers are minimal environments.
They are images that contain what you need to run your application and nothing else.
So in this case, ps command not found, well, that's not going to work.
But I can use ls to see files in this container.
When I use exit, don't worry, you don't shut the container down.
Look, docker ps is still showing it.
I used exit on the docker exec shell that I just started.
And this docker exec shell was a secondary command.
This doesn't show, so it doesn't shut down the entire container.
If you want to shut down a container, use docker stop on your container.
And now it will go away.
Let's check that.
Docker ps doesn't show it anymore.
And that's exactly what I wanted.
We also need to talk about accessing container workloads.
The entry point application, that's a standard application that your container is starting.
And that can expose a port, but this port by default is not accessible from the outside,
because network-wise, containers run in an isolated environment.
Only root containers will have an IP address on the isolated container network.
To access a container, you need port forwarding to expose the container port on the host where it is running.
You do that using a command like docker run minus p 8080 colon 80 nginx,
which will run the container based on an nginx image and expose the container port on port 8080.
You cannot expose ports on containers that have already been started.
Let me show you.
So I'm going to use docker run minus p 8080 colon 80 nginx.
Of course, the first port number, that's where you expose your port, needs to be available.
And oops, let me also use minus d.
Notice that the arguments for docker run should always be behind docker run and not behind the container name.
Because if you put the argument behind the name of the container image,
it's going to be interpreted as an argument for the primary command that your container image is going to run.
Good. Now when I use docker ps, I can see that it has 0000 8080 forwarding to port 80,
meaning that if I would use curl to localhost 8080, I'm getting access.
Oh, no curl? Let's fix that.
sudo apt install curl.
And now I'm going to try it again.
curl localhost, and there we can see welcome to nginx.
It doesn't give anything cool, but it does prove that the container is accessible.