Starting containers
- Many environments are available, let's look at Docker
- As Docker is no longer supported on Red Hat, we'll use Docker on Ubuntu
- To use containers on RHEL 8 and related distributions, use
podman
instead ofdocker
- Use
dnf install -y container-tools
to install podman and related utilities
For the installation please refer the official docs here (opens in a new tab).
Running a container
Running Docker as sudo can be dangerous because it grants elevated privileges to the Docker daemon, which can be exploited by malicious containers or images.
sudo usermod -aG docker $USER
newgrp docker
Adds the current user to the docker group, granting them the ability to run Docker commands without using sudo
docker run -d -p 8080:80 --name="myapache" -v /var/www/html:/var/www/html httpd
Option/Argument | Explanation |
---|---|
docker run | Creates and runs a new container. |
-d | Runs the container in detached mode (in the background). |
-p 8080:80 | Port Forwarding, Maps port 8080 on the host machine to port 80 on the container (useful for web traffic). |
--name="myapache" | Assigns the container a name (myapache ), making it easier to reference later. |
-v /var/www/html:/var/www/html | Mounts a volume from the host (/var/www/html ) to the container's file system at the same path. |
httpd | Specifies the Docker image to use (in this case, the Apache HTTP server image, httpd ). |
Check for the running container
docker ps
Verify if the port is active and listening
ss -tunap | grep 8080
Access the running application from the container using local machine by curl localhost:80
Running another container
docker run -it busybox
will start the busybox container with an interactive terminal to the entrypoint application.
Ctrl-p
,Ctrl-q
to disconnect and keep it runningexit
to stop the current container application- If you were connected to the entrypoint application, the container will stop
- If you were connected to another shell session, the container will continue
- If the docker exit with status code
0
it is considered as normal exit, and if it's non zero then there is some error at the background. - Docker container are minimal configured OS so we can't run all the commands.