An Interest In:
Web News this Week
- March 22, 2024
- March 21, 2024
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
April 21, 2022 03:01 pm GMT
Original Link: https://dev.to/atrooo/run-and-attach-to-a-docker-container-with-one-command-582j
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:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To