Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

How to map host port to docker container port after container is started ?

  1. User

    How to map host port to docker container port after container is started ?

  2. Assistant

    It is not possible to map host port to docker container port after the container is started.

    The port mapping is defined at the time of container creation using the -p or --publish flag with the docker run command.

    However, if you want to expose additional ports on a running container, you can use the docker container update command to add new port mappings.

    For example:

    docker container update --publish-add 8080:80 container_name
    

    This will add a new port mapping, where port 8080 on the host machine is mapped to port 80 in the running container.

  3. User

    How to restart the stopped container

  4. Assistant

    To restart a stopped container in Docker, you can use the docker container start command followed by the container name or ID. For example:

    docker container start container_name
    

    If you want to restart the container with all the options that were used to start it initially, you can use the docker container restart command instead. For example:

    docker container restart container_name
    

    This command will stop the container (if it is running) and then start it again, so it's useful if you want to make sure that the container is in a known state before starting it.

  5. User

    Flags for docker container start container_name

  6. Assistant

    The docker container start command does not have many flags. Below are the flags that can be used with this command:

    • -a or --attach: This flag attaches the container's STDOUT and STDERR streams to the terminal where the docker container start command is being run.

    • -i or --interactive: This flag keeps Standard Input (stdin) open, allowing you to interact with the container's shell.

    • -p or --publish: This flag maps a container's port to a host port. You can specify the host port and the container port using this flag.

    • -d or --detach: This flag starts the container in the background. If this flag is not used, the container starts in the foreground, and the terminal is attached to it.

    • -e or --env: This flag allows you to set environment variables when you start the container.

    • --name: This flag allows you to give a name to the container.

    • --restart: This flag allows you to specify the restart policy for the container. It can be set to no (do not restart), always (restart the container whenever it stops), on-failure (restart the container if it exits with a non-zero exit code), or unless-stopped (restart the container unless it is explicitly stopped).

  7. User

    Flags used for docker run

  8. Assistant

    The docker run command has many flags that allow you to customize how your container is created and runs. Some of the commonly used flags are:

    • -d or --detach: This flag starts the container in the background mode.

    • -p or --publish: This flag maps a container's port to a host port. You can specify the host port and the container port using this flag.

    • -e or --env: This flag allows you to set environment variables when you start the container.

    • --name: This flag allows you to set a name for the container.

    • -v or --volume: This flag allows you to mount a host directory or a named volume into the container.

    • -it or --interactive --tty: This flag starts the container with an interactive shell, and attaches the tty to the container's stdin.

    • --restart: This flag allows you to set the restart policy for the container.

    • -u or --user: This flag specifies which user to run the container as.

    • --network: This flag specifies the network mode for the container.

    These are just some of the many flags that can be used with the docker run command. You can use docker run --help command to see all available options.