• Home
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • Ubuntu
  • Maven
  • Archived
  • About
Docker | Run Docker images (docker run)
  1. Notes
  2. Run an image (docker run)
  3. Set environment variables

  1. Notes
    See these pages for more information:
    Docker CLI: docker run (run a command in a new container)
    https://docs.docker.com/engine/containers/run/
  2. Run an image
    The 'docker run' command has the following syntax:

    Note that docker container run is equivalent to docker run

    The command 'docker run', first creates the container (from the specified image) and then starts it.

    Let's run the nginx image:

    The option '-d' (--detach) run the container in background and print container ID.

    The option '--rm' tells Docker to automatically remove the container when it exits.

    Let's verify that nginx is up and running:

    You can notice that the name of the container (vibrant_mose) is a random name generated by Docker.
    To give a custom name to the container, you can use the option '--name'.

    Note that the container's port (80) is not published to the Docker's host.
    To publish an exposed container's port and map it to a host's port, you can use the option '-p HOST_PORT:CONTAINER_PORT'.
    You can also use the option '-P' to publish all exposed ports to random ports in the Docker's host.

    Let's use the two options ('-p', '--name') and run again the nginx image:

    Let's verify that both the specified port (the container port 80 is mapped to the Docker host port 8080) and the name were used:

    You can use the option '--no-trunc' of the 'ps' command to print the full container ID and the command used to start the nginx process.

    Let's access nginx at the published port 8080:
    Docker nginx - Welcome page
  3. Set environment variables
    The "docker run" command has two options that allow you to set environment variables for your containers:

    Let's try the option "-e" ("--env"):

    The two environment variables were set properly. The applications running inside the container can use them as they would in a regular deployment in a native host.

    In some cases, setting environment variables using the option "-e" can be cumbersome especially if we want to set many of them. Optionally, you can group all the environment variables in a property file and use the option "--env-file" to pass the file to "docker run" command.

    Let's create a property file:

    Let's use the option "--env-file":
© 2025  mtitek