Set Docker Container Start Command With Dockerfile

This post will show how to create an entry point inside a Docker container and set the container to start with a specific command or script.

CMD Statement

When building images with Docker, it is essential to set the container to start with a specific program, script, app or command; otherwise, the container will simply stop.

We set the image to start with a specific program or command using the CMD statement that can only be used once inside a dockerfile.

Below is an example of how I set a container to start and run a simple PowerShell command on Linux using a dockerfile

CMD ["pwsh", "-command" ,"$psversiontable"]

The entire dockerfile is shown below.

FROM ubuntu 
ENV builddir=build
RUN mkdir \${builddir}
RUN apt-get update
RUN apt-get install -y wget apt-transport-https software-properties-common
RUN wget -q
RUN dpkg -i packages-microsoft-prod.deb
RUN apt-get update
RUN add-apt-repository universe
RUN apt-get install -y powershell
CMD ["pwsh", "-command" ,"$psversiontable"]

To learn how to build a Docker image, visit the following post.