• Home
  • Docker
  • Kubernetes
  • Java
  • Ubuntu
  • Maven
  • Big Data
  • CI
  • Install
  • Samples
  • Archived
Docker | Run Docker images (docker run)
  1. Notes
  2. Run an image (docker run)
  3. Set environment variables

  1. Notes
    Please 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:

    The command 'docker run', first create the container and then start it.

    Let's run the nginx image:

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

    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'.

    The second thing you might notice is 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 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