[Docker] Basic commands

Docker is a platform for developers and sysadmins to develop, deploy, and run applications with containers. The use of Linux containers to deploy applications is called containerization. Containers are not new, but their use for easily deploying applications is

First touch

Ensure you’ve installed Docker already by the command:

docker --version

Run a docker image

docker container run httpd:2.4

Note: while httpd is the image name, “2.4” is the version of this image

Listing docker images

docker image ls

Listing docker container

docker container ls -a

Publish port and mapping your host port to your container port when running container

docker container run -p 80:80 httpd:2.4

Run container in interactive mode with command

docker container run -p 80:80 -it httpd:2.4 /bin/bash

Now you’re staying inside the container, run anything like this for fun

apt-get update
apt-get install htop

Dockerfile

Build your own Dockerfile to run all configurations one time

Create a file that name “Dockerfile” (letter D is in uppercase)

nano Dockerfile

with the content like this:

FROM httpd:2.4

EXPOSE 80

RUN apt-get update && apt-get install htop

COPY /my_files/default.html /usr/local/apaches/htdocs/

LABEL maintainer="lelong@home.au"

Build an image with this file

docker image build --tag web-server:1.0 .

note: the last dot “.” in the command above is the path to Dockerfile

Run your own image

docker run -p 80:80 web-server:1.0

Mount your host data into your container data with param “-v”

docker run -p 80:80 --detach -v /my_files:/usr/local/apache2/htdocs web-server:1.0

When your container is running in detach mode, exec something in your container by the command:

docker container exec  <container_name> /bin/bash

 

Leave a Reply

Your email address will not be published. Required fields are marked *