Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 21, 2022 03:01 pm GMT

Run and attach to a Docker container with one command

For more information, see the Docker reference

This command runs a detached (-d), interactive (-i), container from the given image with an allocated tty (-t)

$ docker run -dit image-name23JV89F23

Then you can attach to the container in your current shell

docker attach 23JV89F23

As one command

docker attach $(docker run -dit image-name)

You can make a convenience function to execute this with a short command and the image name as an argument by adding the following to your .bashrc or .zshrc:

# run detached container, start interactive sessiondat() {  image=${1}  if [ -z "$image" ]; then    echo Please provide an image name    return  fi  docker attach $(docker run -dit $image)}

To use:

dat image:tho

Two things to note. First, you'll need to reload your shell for the rc file changes to take effect. Second, if the image does not exist either locally or in your configured registry, docker attach will still run. This was just meant to be a quick and dirty solution for myself, so I'll leave more robust error handling as an exercise for the reader.


Original Link: https://dev.to/atrooo/run-and-attach-to-a-docker-container-with-one-command-582j

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To