Docker Utility
Docker utility is not an official Docker terminology. It is only a way for the instructor to indicate a specific way a Docker environment can be used to perform some commands.
To overwrite the default command "CMD" executed when the container start, we can specify a command after the container name.
To demonstrate the usage of Docker utility, we used the case where we want to use Docker to start a node project on our host machine with npm install
without the need to install NodeJs utilities on our local host machine.
We can create our own node image with a DockerFile
We can build the container using docker build
.
Then, we can run docker run
and specify the node command that will create our node project. The flag -it
is to be able to interact with the process. We also want to create a bind mount to mirror the project created in our Docker container in our localhost.
As we can see in our tree folder, we can find the package.json
file in our host machine, since the /app
folder, where the Docker commands are run in our Docker commands, is shared with the specified folder on localhost.
ENTRYPOINT Instruction
The Docker image created above allows users to run any command they wants inside the container. However, we might want to restrict the type of command the user is allowed to execute. In the example below, the ENTRYPOINT
instruction in the Dockerfile will restrict the user to run commands only starting by npm
. The npm
command will be append to any commands specified by the user following the name of the container.
Compared to CMD
, the instruction ENTRYPOINT
can not be overwritten by any command specified after the container name.
Since we already specified in the Dockerfile npm
as the ENTRYPOINT, we do not have to specify npm init
when running the container, but only the init
part.
We could also run any nmp
command we want. For example, we can type install to run npm install express --save
.
The command above created all necessary files and folders accessible from our localhost.
Docker Compose
In this section of the course, we reused our knowledge of what we have learned in a previous section of the course to create a Docker compose file to automate the following commands:
This Docker compose file contains only one service (one container) and will use the Dockerfile located in the current directory to build the image. The container will be associated to one bind mount.
In the previous section of the course, we learned the docker compose up command to start the services specified in the Docker Compose file. However, this case will introduce us to the use of the docker-compose run
command.
docker-compose run
The docker-compose run
command can be use to start only one specified services included in the Docker compose file.
The command below started the node-utility service and we are allowed to specify the init
command following the name of the service.
Unlike the docker-compose up command, docker-compose run does not remove the container automatically when the container is shut down. We need to specify the --rm
flag.
Some useful security consideration are highlighted in this thread when using Docker with Linux. See here.
Last updated